package top.iridescent.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import top.iridescent.constant.ResultMsg;
import top.iridescent.db.annotation.DataSource;
import top.iridescent.dao.MhPetDao;
import top.iridescent.dao.model.request.MhPetQuery;
import top.iridescent.entity.MhBaseAttribute;
import top.iridescent.entity.MhIncome;
import top.iridescent.entity.MhPet;
import top.iridescent.model.PageModel;
import top.iridescent.model.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.Map;


@Service("mhPetService")
//@DataSource("mh_game_local")
public class MhPetServiceImpl extends ServiceImpl<MhPetDao, MhPet> {
    @Autowired
    private MhIncomeServiceImpl incomeService;

    @Autowired
    private MhBaseAttributeServiceImpl baseAttributeService;
    @Autowired
    private MhExpandAttributeServiceImpl expandAttributeService;

    public Result findPage(Page<Map> page, MhPetQuery param) {
        IPage<MhPet> pageData = baseMapper.findPage(page, param);
        if (pageData != null) {
            pageData.getRecords().forEach(e -> {
                handleDetail(e);
            });
            return Result.ok().setData(new PageModel(pageData.getRecords(), pageData.getTotal(), pageData.getSize(), pageData.getCurrent()));
        } else {
            return Result.error(ResultMsg.DATA_NOT_EXIT);
        }
    }
    public void handleDetail(MhPet data){
        data.setTotalBaseAttr(baseAttributeService.getById(data.getRealBaseAttrId()));
        data.setTotalExpandAttr(expandAttributeService.getById(data.getRealExpandAttrId()));
    }
//    public void handleDetail_local(MhPet data) {
//        Long level = data.getLevel();
//        MhBaseAttribute baseAttr = null;
//        baseAttr.addLevelAttr(Integer.parseInt(level.toString()));
//
//        data.setBaseAttr(baseAttr);
//
//        MhBaseAttribute disAttr =null;
//        data.setDistributeBaseAttr(disAttr);
//
//        data.getTotalBaseAttr().add(data.getBaseAttr());
//        data.getTotalBaseAttr().add(data.getDistributeBaseAttr());
//        data.getTotalBaseAttr().add(data.getEquipmentBaseAttr());
//
//
//        //等级资质
//        MhExpandAttribute levelExpandAttribute = new MhExpandAttribute();
//        levelExpandAttribute.setAttack(Double.parseDouble(handleLevelGong(new BigDecimal(level), data.getChengZhang(), new BigDecimal(data.getGongZi())) + ""));
//        levelExpandAttribute.setBlood(Double.parseDouble(handleLevelXue(new BigDecimal(level), new BigDecimal(data.getTiZi())) + ""));
//        levelExpandAttribute.setMagic(Double.parseDouble(handleLevelMo(new BigDecimal(level), new BigDecimal(data.getFangZi())) + ""));
//        levelExpandAttribute.setArmor(Double.parseDouble(handleLevelFang(data.getFangZi().intValue(), level.intValue()) + ""));
//        levelExpandAttribute.setSpellAttack(Double.parseDouble(handleLevelLing(data.getFangZi().intValue(), level.intValue()) + ""));
//        levelExpandAttribute.setResistance(Double.parseDouble(handleLevelLing(data.getFangZi().intValue(), level.intValue()) + ""));
//        data.setLevelExpandAttribute(levelExpandAttribute);
//
//        //自动属性
//        MhExpandAttribute attrBaseAttrExpandAttribute = new MhExpandAttribute();
//        attrBaseAttrExpandAttribute.setAttack(Double.parseDouble(handleBaseAttrGong(new BigDecimal(baseAttr.getLi()), data.getChengZhang()) + ""));
//        attrBaseAttrExpandAttribute.setBlood(Double.parseDouble(handleBaseAttrXue(new BigDecimal(baseAttr.getTi()), data.getChengZhang())+ ""));
//        attrBaseAttrExpandAttribute.setMagic(Double.parseDouble(handleBaseAttrMo(new BigDecimal(baseAttr.getMo()), data.getChengZhang())+ ""));
//        attrBaseAttrExpandAttribute.setArmor(Double.parseDouble(handleBaseAttrFang(baseAttr.getNai(), data.getChengZhang().intValue())+ ""));
//        attrBaseAttrExpandAttribute.setSpellAttack(Double.parseDouble(handleBaseAttrLing(baseAttr.getTi(), baseAttr.getMo(), baseAttr.getNai(), baseAttr.getLi())+ ""));
//        attrBaseAttrExpandAttribute.setResistance(Double.parseDouble(handleBaseAttrLing(baseAttr.getTi(), baseAttr.getMo(), baseAttr.getNai(), baseAttr.getLi())+ ""));
//        data.setAttrBaseAttrExpandAttribute(attrBaseAttrExpandAttribute);
//        //分配属性
//        MhExpandAttribute attrDistributeAttrExpandAttribute = new MhExpandAttribute();
//        attrDistributeAttrExpandAttribute.setAttack(Double.parseDouble(handleBaseAttrGong(new BigDecimal(disAttr.getLi()), data.getChengZhang()) + ""));
//        attrDistributeAttrExpandAttribute.setBlood(Double.parseDouble(handleBaseAttrXue(new BigDecimal(disAttr.getTi()), data.getChengZhang())+ ""));
//        attrDistributeAttrExpandAttribute.setMagic(Double.parseDouble(handleBaseAttrMo(new BigDecimal(disAttr.getMo()), data.getChengZhang())+ ""));
//        attrDistributeAttrExpandAttribute.setArmor(Double.parseDouble(handleBaseAttrFang(disAttr.getNai(), data.getChengZhang().intValue())+ ""));
//        attrDistributeAttrExpandAttribute.setSpellAttack(Double.parseDouble(handleBaseAttrLing(disAttr.getTi(), disAttr.getMo(), disAttr.getNai(), disAttr.getLi())+ ""));
//        attrDistributeAttrExpandAttribute.setResistance(Double.parseDouble(handleBaseAttrLing(disAttr.getTi(), disAttr.getMo(), disAttr.getNai(), disAttr.getLi())+ ""));
//        data.setAttrDistributeAttrExpandAttribute(attrDistributeAttrExpandAttribute);
//
//
//        data.getTotalExpandAttr().add(data.getLevelExpandAttribute());
//        data.getTotalExpandAttr().add(data.getAttrBaseAttrExpandAttribute());
//        data.getTotalExpandAttr().add(data.getAttrDistributeAttrExpandAttribute());
//        data.getTotalExpandAttr().add(data.getEquipmentExpandAttribute());
//        data.getTotalExpandAttr().add(data.getEquipmentBaseAttrExpandAttribute());
//
//    }

    public int handleBaseAttrGong(BigDecimal li, BigDecimal chengZhang) {
        return li.multiply(chengZhang).intValue();
    }

    public int handleLevelGong(BigDecimal level, BigDecimal chengZhang, BigDecimal zizhi) {
        return zizhi.multiply(level).multiply(new BigDecimal(14).add(new BigDecimal(10).multiply(chengZhang))).divide(new BigDecimal(7500), 2, BigDecimal.ROUND_HALF_UP).intValue();
    }

    public int handleBaseAttrXue(BigDecimal Ti, BigDecimal chengZhang) {
        return Ti.multiply(chengZhang).multiply(new BigDecimal(6)).intValue();
    }

    public int handleLevelXue(BigDecimal level, BigDecimal zizhi) {
        return level.multiply(zizhi).divide(new BigDecimal(1000), 2, BigDecimal.ROUND_HALF_UP).intValue();
    }

    public int handleBaseAttrMo(BigDecimal mo, BigDecimal chengZhang) {
        return mo.multiply(chengZhang).multiply(new BigDecimal(1000)).intValue();
    }

    public int handleLevelMo(BigDecimal level, BigDecimal zizhi) {
        return level.multiply(zizhi).intValue() / 500;
    }

    public int handleBaseAttrLing(int ti, int mo, int nai, int li) {
        return new Double(mo * 0.7 + li * 0.4 + ti * 0.3 + nai * 0.2 ).intValue();
    }

    public int handleLevelLing(int zz, int level) {
        return new Double(level * (zz / 1000 + 4) / 5 ).intValue();
    }

    public int handleBaseAttrFang(int dianshu, int chengzhang) {
        return dianshu * chengzhang * 3;
    }

    public int handleLevelFang(int zizhi, int level) {
        return level * zizhi / 500;
    }


    public Result getById(Long id) {
        MhPet data=super.getById(id);
        data.setTotalExpandAttr(expandAttributeService.getById(data.getRealExpandAttrId()));
        MhBaseAttribute base=baseAttributeService.getById(data.getRealBaseAttrId());
        base.handleTotal();
        data.setTotalBaseAttr(base);
        return Result.ok().setData(data);
    }

    public Result saveInfo(MhPet param) {
        incomeService.saveInfo(new MhIncome("o",param.getMoney().doubleValue(),param.getName()));
        baseAttributeService.save(param.getTotalBaseAttr());
        expandAttributeService.save(param.getTotalExpandAttr());
        param.setRealBaseAttrId(param.getTotalBaseAttr().getId());
        param.setRealExpandAttrId(param.getTotalExpandAttr().getId());

        return Result.ok().setData(super.save(param));
    }

    public Result update(MhPet param) {
        baseAttributeService.updateById(param.getTotalBaseAttr());
        expandAttributeService.updateById(param.getTotalExpandAttr());
        MhPet data = super.getById(param.getId());
        if (ObjectUtils.isEmpty(data)) {
            return Result.error(ResultMsg.DATA_NOT_EXIT);
        }
        return Result.ok().setData(super.updateById(param));
    }

    public Result deleteById(Long id) {
        return Result.ok().setData(super.removeById(id));
    }

}